package cn.com.blueInfo.bpm.person.service.impl;

import cn.com.blueInfo.bpm.person.convert.PostDefConvert;
import cn.com.blueInfo.bpm.person.entity.PostDef;
import cn.com.blueInfo.bpm.person.repository.PostDefRepository;
import cn.com.blueInfo.bpm.person.request.PostDefDTO;
import cn.com.blueInfo.bpm.person.response.PostDefVO;
import cn.com.blueInfo.bpm.person.service.PostDefService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 岗位定义表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class PostDefServiceImpl implements PostDefService {

    @Autowired
    private PostDefRepository postDefRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private PostDefConvert postDefConvert;

    /**
     * 新增岗位定义
     * @param postDefDTO 岗位定义参数
     */
    @Override
    public ResultInfo<Integer> addPostDef(PostDefDTO postDefDTO) {
        PostDef postDef = postDefConvert.dtoToEntity(postDefDTO);
        postDef.setId(snowflakeIdGenerator.nextId());
        Integer addSize = postDefRepository.createPostDef(postDef);
        return ResultInfo.<Integer>builder().data(addSize).message("岗位定义成功新增 {} 条").build();
    }

    /**
     * 批量新增岗位定义
     * @param postDefDTOList 岗位定义参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddPostDef(List<PostDefDTO> postDefDTOList) {
        List<PostDef> postDefList = postDefConvert.dtoToEntity(postDefDTOList);
        for (PostDef postDef : postDefList) postDef.setId(snowflakeIdGenerator.nextId());
        Integer addSize = postDefRepository.batchCreatePostDef(postDefList);
        return ResultInfo.<Integer>builder().data(addSize).message("岗位定义成功新增 {} 条").build();
    }

    /**
     * 删除岗位定义
     * @param postDefId 岗位定义ID
     */
    @Override
    public ResultInfo<Integer> removePostDef(Long postDefId) {
        Integer removeSize = postDefRepository.deletePostDef(postDefId);
        return ResultInfo.<Integer>builder().data(removeSize).message("岗位定义成功删除 {} 条").build();
    }

    /**
     * 批量删除岗位定义
     * @param postDefIdList 岗位定义ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemovePostDef(List<Long> postDefIdList) {
        Integer removeSize = postDefRepository.batchDeletePostDef(postDefIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("岗位定义成功删除 {} 条").build();
    }

    /**
     * 更新部分岗位定义
     * @param postDefUpdatePartialDTO 岗位定义参数
     */
    @Override
    public ResultInfo<Integer> modifyPostDefPartial(PostDefDTO.UpdatePartialDTO postDefUpdatePartialDTO) {
        PostDef postDef = postDefConvert.dtoToEntity(postDefUpdatePartialDTO);
        Integer modifySize = postDefRepository.updatePostDefPartial(postDef);
        return ResultInfo.<Integer>builder().data(modifySize).message("岗位定义成功更新 {} 条").build();
    }

    /**
     * 更新全部岗位定义
     * @param postDefDTO 岗位定义参数
     */
    @Override
    public ResultInfo<Integer> modifyPostDef(PostDefDTO postDefDTO) {
        PostDef postDef = postDefConvert.dtoToEntity(postDefDTO);
        Integer modifySize = postDefRepository.updatePostDef(postDef);
        return ResultInfo.<Integer>builder().data(modifySize).message("岗位定义成功更新 {} 条").build();
    }

    /**
     * 查询一条岗位定义
     * @param postDefId 岗位定义ID
     */
    @Override
    public ResultInfo<PostDefVO> queryPostDefById(Long postDefId) {
        PostDef postDef = postDefRepository.getPostDefById(postDefId);
        PostDefVO postDefVO = postDefConvert.entityToVO(postDef);
        return ResultInfo.<PostDefVO>builder().data(postDefVO).message("岗位定义查询成功").build();
    }

    /**
     * 分页查询岗位定义
     * @param postDefQueryDTO 岗位定义参数
     */
    @Override
    public ResultInfo<Page<PostDefVO>> queryPostDefPage(PostDefDTO.QueryDTO postDefQueryDTO) {
        Page<PostDef> postDefPage = postDefRepository.getPostDefPage(postDefQueryDTO);
        Page<PostDefVO> resultData = postDefConvert.convertPage(postDefPage);
        return ResultInfo.<Page<PostDefVO>>builder()
                .data(resultData)
                .message("岗位定义分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 岗位定义 数量
     * @param postDefQueryDTO 岗位定义参数
     */
    @Override
    public ResultInfo<Long> queryPostDefCount(PostDefDTO.QueryDTO postDefQueryDTO) {
        Long resultCount = postDefRepository.getPostDefCount(postDefQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("岗位定义数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 岗位定义
     * @param postDefQueryDTO 岗位定义参数
     */
    @Override
    public ResultInfo<List<PostDefVO>> queryPostDefList(PostDefDTO.QueryDTO postDefQueryDTO) {
        List<PostDef> postDefList = postDefRepository.getPostDefList(postDefQueryDTO);
        List<PostDefVO> resultData = postDefConvert.entityListToVoList(postDefList);
        return ResultInfo.<List<PostDefVO>>builder()
                .data(resultData)
                .message("岗位定义分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
